//
//  DataSummaryCellViewModel.swift
//  TimeBoat
//
//  Created by bo wang on 2022/8/24.
//  Copyright © 2022 Time Boat. All rights reserved.
//

import UIKit
import Rswift

class DataSummaryCellViewModel: NSObject {
    
    enum Style {
        case normal
        case large(_ aspectRatio: CGSize)
        
        var ratio: CGSize {
            switch self {
            case .normal: return CGSize(width: 170, height: 183)
            case .large(let ratio): return ratio
            }
        }
    }
    
    enum CellSubject {
        case sportRing
        case heartRate
        case sleep
        case temperature
        case bloodPressure
        case bloodOxygen
        case sport
        case location
        case ad
        
        var defaultStyle: Style {
            switch self {
            case .sportRing: return .large(CGSize(width: 353, height: 181))
            case .heartRate, .sleep, .temperature, .bloodPressure, .bloodOxygen: return .normal
            case .sport: return .large(CGSize(width: 353, height: 165))
            case .location: return .normal
            case .ad: return .large(CGSize(width: 353, height: 165))
            }
        }
        
        var chartSubject: ChartsSubject? {
            switch self {
            case .sportRing: return .steps
            case .heartRate: return .heartRate
            case .sleep: return .sleep
            case .temperature: return .temperature
            case .bloodPressure: return .bloodPressure
            case .bloodOxygen: return .bloodOxygen
            case .sport: return .sports
            case .location: return nil
            case .ad: return nil
            }
        }
    }
    
    private(set) var subject: CellSubject
    private lazy var style: Style = subject.defaultStyle
    
    init(_ subject: CellSubject) {
        self.subject = subject
    }
    
    func itemSize(for layout: UICollectionViewFlowLayout) -> CGSize {
        let fullWidth = UIScreen.main.bounds.width
        
        let left = layout.sectionInset.left
        let right = layout.sectionInset.right
        let spacing = layout.minimumInteritemSpacing
        
        let width: CGFloat = {
            switch style {
            case .normal: return (fullWidth - left - right - spacing) / 2
            case .large: return fullWidth - left - right
            }
        }()
        let height = width * style.ratio.height / style.ratio.width
        return CGSize(width: width, height: height)
    }
    
    func cell(for collectionView: UICollectionView, indexPath: IndexPath) -> DataSummaryCell {
        switch subject {
        case .sportRing:
            return collectionView.dequeueReusableCell(withReuseIdentifier:R.reuseIdentifier.dataSummarySportRingCell, for: indexPath) as! DataSummaryCell
            
//            return collectionView.dequeueCell(R.reuseIdentifier.dataSummarySportRingCell, for: indexPath)
        case .heartRate:
            return collectionView.dequeueReusableCell(withReuseIdentifier:R.reuseIdentifier.dataSummaryHeartRateCell, for: indexPath) as! DataSummaryCell

//            return collectionView.dequeueCell(R.reuseIdentifier.dataSummaryHeartRateCell, for: indexPath)
        case .sleep:
            return collectionView.dequeueReusableCell(withReuseIdentifier:R.reuseIdentifier.dataSummarySleepCell, for: indexPath) as! DataSummaryCell

//            return collectionView.dequeueCell(R.reuseIdentifier.dataSummarySleepCell, for: indexPath)
        case .temperature:
            return collectionView.dequeueReusableCell(withReuseIdentifier:R.reuseIdentifier.dataSummaryTemperatureCell, for: indexPath) as! DataSummaryCell

//            return collectionView.dequeueCell(R.reuseIdentifier.dataSummaryTemperatureCell, for: indexPath)
        case .bloodPressure:
            return collectionView.dequeueReusableCell(withReuseIdentifier:R.reuseIdentifier.dataSummaryBloodPressureCell, for: indexPath) as! DataSummaryCell

//            return collectionView.dequeueCell(R.reuseIdentifier.dataSummaryBloodPressureCell, for: indexPath)
        case .bloodOxygen:
            return collectionView.dequeueReusableCell(withReuseIdentifier:R.reuseIdentifier.dataSummaryBloodOxygenCell, for: indexPath) as! DataSummaryCell

//            return collectionView.dequeueCell(R.reuseIdentifier.dataSummaryBloodOxygenCell, for: indexPath)
        case .sport:
            return collectionView.dequeueReusableCell(withReuseIdentifier:R.reuseIdentifier.dataSummarySportCell, for: indexPath) as! DataSummaryCell

//            return collectionView.dequeueCell(R.reuseIdentifier.dataSummarySportCell, for: indexPath)
        case .location:
            return collectionView.dequeueReusableCell(withReuseIdentifier:R.reuseIdentifier.dataSummaryLocationCell, for: indexPath) as! DataSummaryCell

//            return collectionView.dequeueCell(R.reuseIdentifier.dataSummaryLocationCell, for: indexPath)
        case .ad:
            return collectionView.dequeueReusableCell(withReuseIdentifier:R.reuseIdentifier.dataSummaryADCell, for: indexPath) as! DataSummaryCell

//            return collectionView.dequeueCell(R.reuseIdentifier.dataSummaryADCell, for: indexPath)
        }
    }
}
